package com.zmj.sy.mom.srv.aps.service;

import com.zmj.sy.mom.srv.aps.bean.entity.base.ProcessChainItem;
import com.zmj.sy.mom.srv.aps.bean.vo.base.BaseIdReqVo;
import com.zmj.sy.mom.srv.aps.bean.vo.base.BaseListResVo;
import com.zmj.sy.mom.srv.aps.bean.vo.processchainitem.*;
import com.zmj.sy.mom.srv.aps.mapper.ProcessChainItemMapper;
import com.zmj.sy.mom.srv.aps.utils.MkBeanUtils;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

@RequiredArgsConstructor
@Service
public class ProcessChainItemService {

    private final ProcessChainItemMapper processChainItemMapper;

    public BaseListResVo<ProcessChainItemListResVo> list(ProcessChainItemListReqVo reqVo) {
        List<ProcessChainItem> list = processChainItemMapper.lambdaQuery()
                .eq(ProcessChainItem::getChainId, reqVo.getChainId())
                .orderByAsc(ProcessChainItem::getSeq)
                .list();

        return BaseListResVo.of(list, ProcessChainItemListResVo.class);
    }

    @Transactional
    public void add(ProcessChainItemAddReqVo reqVo) {
        ProcessChainItem item = MkBeanUtils.copyProperties(reqVo, ProcessChainItem.class);
        Integer count = processChainItemMapper.lambdaQuery().eq(ProcessChainItem::getChainId, reqVo.getChainId()).count();
        item.setSeq(count + 1);
        processChainItemMapper.insert(item);
    }

    @Transactional
    public void edit(ProcessChainItemEditReqVo reqVo) {
        ProcessChainItem item = MkBeanUtils.copyProperties(reqVo, ProcessChainItem.class);
        processChainItemMapper.updateById(item);
    }

    @Transactional
    public void delete(BaseIdReqVo reqVo) {

        ProcessChainItem item = processChainItemMapper.selectById(reqVo.getId());
        processChainItemMapper.deleteById(reqVo.getId());

        List<ProcessChainItem> list = processChainItemMapper.lambdaQuery().eq(ProcessChainItem::getChainId, item.getChainId()).orderByAsc(ProcessChainItem::getSeq).list();
        for (int i = 0; i < list.size(); i++) {
            list.get(i).setSeq(i + 1);
            processChainItemMapper.updateById(list.get(i));
        }
    }

    @Transactional
    public void move(ProcessChainItemMoveReqVo reqVo) {
        ProcessChainItem source = processChainItemMapper.selectById(reqVo.getId());


        ProcessChainItem target = null;
        if (reqVo.getType().equals(1)) {
            target = processChainItemMapper.lambdaQuery()
                    .eq(ProcessChainItem::getChainId, source.getChainId())
                    .lt(ProcessChainItem::getSeq, source.getSeq())
                    .orderByDesc(ProcessChainItem::getSeq)
                    .last(" limit 1 ")
                    .one();
        } else {
            target = processChainItemMapper.lambdaQuery()
                    .eq(ProcessChainItem::getChainId, source.getChainId())
                    .gt(ProcessChainItem::getSeq, source.getSeq())
                    .orderByAsc(ProcessChainItem::getSeq)
                    .last(" limit 1 ")
                    .one();
        }
        if(target == null){
            return ;
        }

        Integer upSeq = source.getSeq();
        source.setSeq(target.getSeq());
        target.setSeq(upSeq);

        processChainItemMapper.updateById(source);
        processChainItemMapper.updateById(target);

    }
}
